package com.yxh.lib_base.base

import android.content.Intent
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.KeyboardUtils
import com.gyf.immersionbar.ImmersionBar
import com.yxh.lib_base.base.loading.ILoading
import com.yxh.lib_base.base.viewmodel.BaseViewModel
import com.yxh.lib_base.ext.xpopup.dismissLoadingExt
import com.yxh.lib_base.ext.xpopup.showLoadingExt

/**
 * 时间　: 2022/08/13
 * 作者　: yuan_xh
 * 描述　: 你项目中的Activity基类，在这里实现显示弹窗，吐司，还有加入自己的需求操作 ，如果不想用Databind，请继承
 */
abstract class BaseActivity : AppCompatActivity(), ILoading {

    /**
     * 当前Fragment绑定的视图布局
     */
    abstract val mBinding: ViewBinding

    //点击空白处是否隐藏键盘 默认true
    private var isClickBlankHideKeyboard = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initData()
        onCreated()
        initStatusBar {  }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        initData()
    }

    abstract fun onCreated()

    open fun initData(){}

    /**
     * 打开等待框
     */
    override fun showLoading(message: String) {
        showLoadingExt(message)
    }

    /**
     * 关闭等待框
     */
    override fun dismissLoading() {
        dismissLoadingExt()
    }


    /**
     * 将非该Activity绑定的ViewModel添加 loading回调 防止出现请求时不显示 loading 弹窗bug
     * @param viewModels Array<out BaseViewModel>
     */
    protected fun addLoadingObserve(vararg viewModels: BaseViewModel) {
        viewModels.forEach { viewModel ->
            //显示弹窗
            viewModel.loadingChange.showDialog.observe(this) {
                showLoading(it)
            }
            //关闭弹窗
            viewModel.loadingChange.dismissDialog.observe(this) {
                dismissLoading()
            }
        }
    }


    protected fun initStatusBar(_immersionBar: ImmersionBar.() -> Unit) {
        var immersionBar = ImmersionBar.with(this)
            .statusBarDarkFont(true)
        _immersionBar.invoke(immersionBar)
        immersionBar.init()
    }

    /**
     * 点击空白处隐藏键盘
     */
    protected fun clickBlankHideKeyboard(isHide: Boolean = true) {
        isClickBlankHideKeyboard = isHide
    }

    /**
     * 点击空白处 隐藏键盘
     */
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (ev.action === MotionEvent.ACTION_DOWN && isClickBlankHideKeyboard) {
            val v = currentFocus
            if (isShouldHideKeyboard(v, ev)) {
                KeyboardUtils.hideSoftInput(this)
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    /**
     * dispatchTouchEvent 使用
     * @param v
     * @param event
     * @return
     */
    private fun isShouldHideKeyboard(v: View?, event: MotionEvent): Boolean {
        return if (v != null && v is EditText) {
            val l = intArrayOf(0, 0)
            v.getLocationInWindow(l)
            val left = l[0]
            val top = l[1]
            val bottom = top + v.getHeight()
            val right = left + v.getWidth()
            event.x <= left.toFloat() || event.x >= right.toFloat() || event.y <= top.toFloat() || event.y >= bottom.toFloat()
        } else {
            false
        }
    }
}